import pymysql
db = pymysql.connect("localhost", "test20170307", "test20170307", "scalers_forum_db",charset="utf8")
cursor=db.cursor()

def p(str):
    print(str)

def getPreValue(preName):
    cursor = db.cursor()
    cursor.execute("select pre_value from pre_table where pre_name = %s",preName)
    db.commit()
    result=cursor.fetchone()
    if result == None:
        p("L111 "+preName+"is not in table!")
    else:
        results=str(result).split("'")
        result=results[1]
    cursor.close()
    return result

def addPreToPreTable(preName,preValue):
    cursor=db.cursor()
    sqlStr="select pre_name,pre_value from pre_table where pre_name = %s"
    cursor.execute(sqlStr,preName)
    if cursor.rowcount==0:
        # 如果has_unfinished_work条目不存在，则创建，初始pre_value为false。
        instrtIntoPreTableSql = "INSERT INTO `pre_table` (`pre_name`, `pre_value`) VALUES (%s, %s)"
        cursor = db.cursor()
        cursor.execute(instrtIntoPreTableSql, (preName, preValue))
        db.commit()
        cursor.close()
    else:
        sqlStr="update pre_table set pre_value = %s where pre_name = %s"
        cursor.execute(sqlStr,(preValue,preName))
        db.commit()
        cursor.close()
        p("L101 Update " + preName + " successfully!")
        p("L102 value of "+preName+" is : "+getPreValue(preName))

str="1.成长会2017必知必会我假设你来到成长会是真心想要成长进步的，而不是来寻找安慰的，"
p("repr(str)")
p(repr(str))
strUtf8=str.encode("utf8")
p("repr(strUtf8)")
p(repr(strUtf8))
addPreToPreTable("chinese_write_test",str)

cursor.close()